package sein.android.hopenote;

import sein.android.hopenote.Const.HopeNoteDb;
import android.R.integer;
import android.R.string;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class User_Add extends Activity {
	
	private DBAdapter HnopenHelper;
	private SQLiteDatabase sdb;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_add);
		
		final Button btn_Add = (Button)findViewById(R.id.btnAdd);
		final EditText txt_Name = (EditText)findViewById(R.id.etName);
		final DatePicker dPk_Birthday = (DatePicker)findViewById(R.id.dpBirthday);
		final RadioGroup SexGroup = (RadioGroup)findViewById(R.id.rgSex);
		final RadioButton Sex =(RadioButton)findViewById(SexGroup.getCheckedRadioButtonId());
		
		
		
		btn_Add.setOnClickListener(new Button.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				HnopenHelper = new DBAdapter(getApplicationContext());				
		        sdb = HnopenHelper.getWritableDatabase();
		        		        
	        	if(txt_Name.getText().toString().replace(" ", "").equals(""))
		        {
		        	Toast.makeText(User_Add.this,"사용자를 입력하세요",Toast.LENGTH_LONG ).show();       	
		        }	        	
		        else {
			        //반환할 컬럼 항목 _id 컬럼 반드시 포함 해야 함
			        String getColumns[] = {HopeNoteDb.COL_USER};
			        String qWhere = HopeNoteDb.COL_USER + " = '" + txt_Name.getText() + "'";
			         String sSex = null;
			         String sBirthDay = null;
			        
			        //쿼리
			        //테이블명, 반환할 컬럼항목, where like, group by ,having ,order by, limit )
			        Cursor c = sdb.query(HopeNoteDb.TB_USER, getColumns, qWhere,null ,null, null, null, null);
			        	
			        if ( c.getCount() == 0 )
			        {		        		
			        	if ( Sex.getText().toString().trim().equals("남")){
		        			sSex = "0";
		        		}
		        		else {
		        			sSex  = "1";
		        		}
			        	
			        	sBirthDay =  Integer.toString(dPk_Birthday.getYear()) + "-" + Integer.toString(dPk_Birthday.getMonth()) + "-" + Integer.toString(dPk_Birthday.getDayOfMonth());
			        	//Log.i("BirthDay", sBirthDay);
			        	sdb.execSQL("INSERT INTO " + HopeNoteDb.TB_USER + " ( "
		        				+ HopeNoteDb.COL_USER + "," + HopeNoteDb.COL_SEX + "," + HopeNoteDb.COL_AGE + " ) VALUES ( '"
		        				+ txt_Name.getText() + "','" + sSex + "','" + sBirthDay + "');"
		        				);		        			        	
			        	Toast.makeText(User_Add.this,"사용자가 저장 되었습니다.",Toast.LENGTH_LONG ).show();
			        }
			        else
			        {
			        	 Toast.makeText(User_Add.this,"이미 등록된 사용자 입니다.",Toast.LENGTH_LONG ).show();		        	
			        }
			        
			        sdb.close();
		        }
				
			}
		});
				
		
		
		
	}
	
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub		
		//super.onBackPressed();
		Note_Input_Tab parent = ((Note_Input_Tab)getParent());
		parent.onBackPressed();
	}

}
